﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BYManageInfoSystem
{
    public partial class Frm_PaicheAddFac : Form
    {
        private DGBY.BLL.PaiChe_FacTab m_bllPaiChe_FacTab = new DGBY.BLL.PaiChe_FacTab();
        private DGBY.BLL.FacAddrTab m_bllFacAddrTab = new DGBY.BLL.FacAddrTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DGBY.BLL.FacConPeoAndTelTab m_bllFacConPeoAndTelTab = new DGBY.BLL.FacConPeoAndTelTab();
        private int m_nPaicheID = 0;
        private int m_nSelectPaiChe_FacTabID = 0;

        public Frm_PaicheAddFac()
        {
            InitializeComponent();
        }

        public Frm_PaicheAddFac(int nPaicheID)
        {
            m_nPaicheID = nPaicheID;
            InitializeComponent();
        }

        private void InitCombox()
        {
            DataSet ds = m_bllFactoryDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxChooseFactory.DataSource = ds.Tables[0];
                this.comboBoxChooseFactory.DisplayMember = "工厂简称";
                this.comboBoxChooseFactory.ValueMember = "FactoryDataTabID";
                this.comboBoxChooseFactory.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxChooseFactory.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxChooseFactory.SelectedIndex = 1;
            }
            
            //ds = m_bllFacAddrTab.GetList("");
            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    this.comboBoxAddr.DataSource = ds.Tables[0];
            //    this.comboBoxAddr.DisplayMember = "FacAddr";
            //    this.comboBoxAddr.ValueMember = "FacAddrTabID";
            //    this.comboBoxAddr.AutoCompleteSource = AutoCompleteSource.ListItems;
            //    this.comboBoxAddr.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //    this.comboBoxAddr.SelectedIndex = 0;
            //}
            //ds = m_bllFacConPeoAndTelTab.GetList("");
            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    this.comboBoxPerson.DataSource = ds.Tables[0];
            //    this.comboBoxPerson.DisplayMember = "FacConPeoName";
            //    this.comboBoxPerson.ValueMember = "FacConPeoAndTelTabID";
            //    this.comboBoxPerson.AutoCompleteSource = AutoCompleteSource.ListItems;
            //    this.comboBoxPerson.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //    this.comboBoxPerson.SelectedIndex = 0;
            //}
        }
        private void Frm_PaicheAddFac_Load(object sender, EventArgs e)
        {
            InitCombox();
            ShowAllRecordInDataGridView();

        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.PaiChe_FacTab modelPaiChe_FacTab = new DGBY.Model.PaiChe_FacTab();
                modelPaiChe_FacTab.PaiCheTabNewID = m_nPaicheID;
             

                if (this.comboBoxChooseFactory.SelectedValue != null)
                    modelPaiChe_FacTab.FactoryDataTabID = Convert.ToInt32(this.comboBoxChooseFactory.SelectedValue.ToString(), 10);

                if (this.comboBoxAddr.SelectedValue != null)
                    modelPaiChe_FacTab.FacAddrTabID = Convert.ToInt32(this.comboBoxAddr.SelectedValue.ToString(), 10);


                if (this.comboBoxPerson.SelectedValue != null)
                    modelPaiChe_FacTab.FacConPeoAndTelTabID = Convert.ToInt32(this.comboBoxPerson.SelectedValue.ToString(), 10);

                m_bllPaiChe_FacTab.Add(modelPaiChe_FacTab);

                ShowAllRecordInDataGridView();
                MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllPaiChe_FacTab.GetModel(m_nSelectPaiChe_FacTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                DGBY.Model.PaiChe_FacTab modelPaiChe_FacTab = m_bllPaiChe_FacTab.GetModel(m_nSelectPaiChe_FacTabID);
                if (this.comboBoxChooseFactory.SelectedValue != null)
                    modelPaiChe_FacTab.FactoryDataTabID = Convert.ToInt32(this.comboBoxChooseFactory.SelectedValue.ToString(), 10);

                if (this.comboBoxAddr.SelectedValue != null)
                    modelPaiChe_FacTab.FacAddrTabID = Convert.ToInt32(this.comboBoxAddr.SelectedValue.ToString(), 10);


                if (this.comboBoxPerson.SelectedValue != null)
                    modelPaiChe_FacTab.FacConPeoAndTelTabID = Convert.ToInt32(this.comboBoxPerson.SelectedValue.ToString(), 10);

                m_bllPaiChe_FacTab.Update(modelPaiChe_FacTab);

                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (m_bllPaiChe_FacTab.GetModel(m_nSelectPaiChe_FacTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllPaiChe_FacTab.Delete(m_nSelectPaiChe_FacTabID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);


        }

        private void dataGridViewFactoryAddrList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strPaiChe_FacTabID = this.dataGridViewFactoryAddrList.Rows[this.dataGridViewFactoryAddrList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectPaiChe_FacTabID = Convert.ToInt32(strPaiChe_FacTabID, 10);
            cellclickFacConPeoAndTelTab();
        }
        private void ShowAllRecordInDataGridView()
        {
            dataGridViewFactoryAddrList.DataSource = null;
            DataSet m_ds = m_bllPaiChe_FacTab.GetFacInfo("PaiCheTabNewID ='" + m_nPaicheID + "'");
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewFactoryAddrList.DataSource = m_ds.Tables[0];
                dataGridViewFactoryAddrList.Columns[0].Visible = false;
                dataGridViewFactoryAddrList.Columns[1].Visible = false;
            }
        }
        private void cellclickFacConPeoAndTelTab()
        {
            DGBY.Model.PaiChe_FacTab modelPaiChe_FacTab = m_bllPaiChe_FacTab.GetModel(m_nSelectPaiChe_FacTabID);
            if (modelPaiChe_FacTab != null)
            {
                if (modelPaiChe_FacTab.FactoryDataTabID != null)
                    this.comboBoxChooseFactory.SelectedValue = modelPaiChe_FacTab.FactoryDataTabID;
                if (modelPaiChe_FacTab.FacConPeoAndTelTabID != null)
                    this.comboBoxPerson.SelectedValue = modelPaiChe_FacTab.FacConPeoAndTelTabID;
                if (modelPaiChe_FacTab.FacAddrTabID != null)
                    this.comboBoxAddr.SelectedValue = modelPaiChe_FacTab.FacAddrTabID;
      
            }
            
        }
        private void comboBoxChooseFactory_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string strCompID = this.comboBoxChooseFactory.SelectedValue.ToString();
                
                DataSet ds = m_bllFacConPeoAndTelTab.GetConPListByCompID(strCompID);
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    this.comboBoxPerson.DataSource = ds.Tables[0];
                    this.comboBoxPerson.DisplayMember = "工厂联系人";
                    this.comboBoxPerson.ValueMember = "FacConPeoAndTelTabID";
                    this.comboBoxPerson.AutoCompleteSource = AutoCompleteSource.ListItems;
                    this.comboBoxPerson.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    this.comboBoxPerson.SelectedIndex = 0;
                }
                ds = m_bllFacAddrTab.GetAddrListByCompID(strCompID);
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    this.comboBoxAddr.DataSource = ds.Tables[0];
                    this.comboBoxAddr.DisplayMember = "工厂地址";
                    this.comboBoxAddr.ValueMember = "FacAddrTabID";
                    this.comboBoxAddr.AutoCompleteSource = AutoCompleteSource.ListItems;
                    this.comboBoxAddr.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    this.comboBoxAddr.SelectedIndex = 0;
                }
            }
            catch { }
        }
    }
}
